package com.jungle;

import com.jungle.plugin.MybatisFscPlugin;
import com.jungle.task.executor.CompensateTaskExecutor;
import com.jungle.task.executor.ConvertTaskExecutor;
import com.jungle.task.executor.DefaultConvertTaskExecutor;
import com.jungle.task.executor.TaskExecutor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author 李岩 at 2021/10/22.
 */
@ConditionalOnClass(value = {
    SqlSessionFactory.class,
})
@Configuration
public class MybatisFscPluginAutoConfigure {
    @Bean
    @ConditionalOnMissingBean(
        value = ConvertTaskExecutor.class
    )
    public DefaultConvertTaskExecutor getDefaultConvertTaskExecutor(){
        return new DefaultConvertTaskExecutor();
    }

    @Bean
    @ConditionalOnBean(CompensateTaskExecutor.class)
    public FscContext getCompensateContext(final List<TaskExecutor<?,?>> executors){
        return new FscContext(executors,true);
    }

    @Bean
    @ConditionalOnMissingBean(CompensateTaskExecutor.class)
    public FscContext getNoCompensateContext(final List<TaskExecutor<?,?>> executors){
        return new FscContext(executors,false);
    }

    @Bean
    public MybatisFscPlugin getPlugin(final FscContext context){
        return new MybatisFscPlugin(context);
    }
}
